package com.appbee.user.client.startup;

import javax.inject.Inject;

import com.appbee.core.client.startup.StartupChain;
import com.appbee.core.client.startup.StartupLink;
import com.appbee.user.client.gin.WebUserProxyProvider;
import com.appbee.user.shared.proxy.WebUserProxy;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.ui.HasWidgets.ForIsWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;

public class UserStartupLink implements StartupLink
{
	private final IUserStartupLoader dataRequest;

	@Inject
	public UserStartupLink(IUserStartupLoader dataRequest)
	{
		this.dataRequest = dataRequest;
	}

	@Override
	public void start(ForIsWidget container, StartupChain chain)
	{
		// User request
		Request<WebUserProxy> userRequest = dataRequest.getCurrentUser(Location
				.getHref());
		userRequest.with("member");
		userRequest.to(new Receiver<WebUserProxy>()
		{

			@Override
			public void onSuccess(WebUserProxy user)
			{
				GWT.log("User data loaded");
				WebUserProxyProvider.setUser(user);
			}

		});
		chain.start(container);
	}

}
